/*DONE* Table [dbo].[<CompanyName>$Purch_ Cr_ Memo Line$437dbf0e-84ff-417a-965d-ed2bb9650972]  1/2  Script Date: 29.7.2020 10:30:57 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Purch_ Cr_ Memo Line$437dbf0e-84ff-417a-965d-ed2bb9650972]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Purch_ Cr_ Memo Line$437dbf0e-84ff-417a-965d-ed2bb9650972](
	[timestamp] [timestamp] NOT NULL,
	[Document No_] [nvarchar](20) NOT NULL,
	[Line No_] [int] NOT NULL,
	[Buy-from Vendor No_] [nvarchar](20) NOT NULL,
	[Type] [int] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[Location Code] [nvarchar](10) NOT NULL,
	[Posting Group] [nvarchar](20) NOT NULL,
	[Expected Receipt Date] [datetime] NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[Description 2] [nvarchar](50) NOT NULL,
	[Unit of Measure] [nvarchar](50) NOT NULL,
	[Quantity] [decimal](38, 20) NOT NULL,
	[Direct Unit Cost] [decimal](38, 20) NOT NULL,
	[Unit Cost (LCY)] [decimal](38, 20) NOT NULL,
	[VAT _] [decimal](38, 20) NOT NULL,
	[Line Discount _] [decimal](38, 20) NOT NULL,
	[Line Discount Amount] [decimal](38, 20) NOT NULL,
	[Amount] [decimal](38, 20) NOT NULL,
	[Amount Including VAT] [decimal](38, 20) NOT NULL,
	[Unit Price (LCY)] [decimal](38, 20) NOT NULL,
	[Allow Invoice Disc_] [tinyint] NOT NULL,
	[Gross Weight] [decimal](38, 20) NOT NULL,
	[Net Weight] [decimal](38, 20) NOT NULL,
	[Units per Parcel] [decimal](38, 20) NOT NULL,
	[Unit Volume] [decimal](38, 20) NOT NULL,
	[Appl_-to Item Entry] [int] NOT NULL,
	[Shortcut Dimension 1 Code] [nvarchar](20) NOT NULL,
	[Shortcut Dimension 2 Code] [nvarchar](20) NOT NULL,
	[Job No_] [nvarchar](20) NOT NULL,
	[Indirect Cost _] [decimal](38, 20) NOT NULL,
	[Order No_] [nvarchar](20) NOT NULL,
	[Order Line No_] [int] NOT NULL,
	[Pay-to Vendor No_] [nvarchar](20) NOT NULL,
	[Inv_ Discount Amount] [decimal](38, 20) NOT NULL,
	[Vendor Item No_] [nvarchar](50) NOT NULL,
	[Gen_ Bus_ Posting Group] [nvarchar](20) NOT NULL,
	[Gen_ Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[VAT Calculation Type] [int] NOT NULL,
	[Transaction Type] [nvarchar](10) NOT NULL,
	[Transport Method] [nvarchar](10) NOT NULL,
	[Attached to Line No_] [int] NOT NULL,
	[Entry Point] [nvarchar](10) NOT NULL,
	[Area] [nvarchar](10) NOT NULL,
	[Transaction Specification] [nvarchar](10) NOT NULL,
	[Tax Area Code] [nvarchar](20) NOT NULL,
	[Tax Liable] [tinyint] NOT NULL,
	[Tax Group Code] [nvarchar](20) NOT NULL,
	[Use Tax] [tinyint] NOT NULL,
	[VAT Bus_ Posting Group] [nvarchar](20) NOT NULL,
	[VAT Prod_ Posting Group] [nvarchar](20) NOT NULL,
	[Blanket Order No_] [nvarchar](20) NOT NULL,
	[Blanket Order Line No_] [int] NOT NULL,
	[VAT Base Amount] [decimal](38, 20) NOT NULL,
	[Unit Cost] [decimal](38, 20) NOT NULL,
	[System-Created Entry] [tinyint] NOT NULL,
	[Line Amount] [decimal](38, 20) NOT NULL,
	[VAT Difference] [decimal](38, 20) NOT NULL,
	[VAT Identifier] [nvarchar](20) NOT NULL,
	[IC Partner Ref_ Type] [int] NOT NULL,
	[IC Partner Reference] [nvarchar](20) NOT NULL,
	[Prepayment Line] [tinyint] NOT NULL,
	[IC Partner Code] [nvarchar](20) NOT NULL,
	[Posting Date] [datetime] NOT NULL,
	[Pmt_ Discount Amount] [decimal](38, 20) NOT NULL,
	[Dimension Set ID] [int] NOT NULL,
	[Job Task No_] [nvarchar](20) NOT NULL,
	[Job Line Type] [int] NOT NULL,
	[Job Unit Price] [decimal](38, 20) NOT NULL,
	[Job Total Price] [decimal](38, 20) NOT NULL,
	[Job Line Amount] [decimal](38, 20) NOT NULL,
	[Job Line Discount Amount] [decimal](38, 20) NOT NULL,
	[Job Line Discount _] [decimal](38, 20) NOT NULL,
	[Job Unit Price (LCY)] [decimal](38, 20) NOT NULL,
	[Job Total Price (LCY)] [decimal](38, 20) NOT NULL,
	[Job Line Amount (LCY)] [decimal](38, 20) NOT NULL,
	[Job Line Disc_ Amount (LCY)] [decimal](38, 20) NOT NULL,
	[Job Currency Factor] [decimal](38, 20) NOT NULL,
	[Job Currency Code] [nvarchar](20) NOT NULL,
	[Deferral Code] [nvarchar](10) NOT NULL,
	[Prod_ Order No_] [nvarchar](20) NOT NULL,
	[Variant Code] [nvarchar](10) NOT NULL,
	[Bin Code] [nvarchar](20) NOT NULL,
	[Qty_ per Unit of Measure] [decimal](38, 20) NOT NULL,
	[Unit of Measure Code] [nvarchar](10) NOT NULL,
	[Quantity (Base)] [decimal](38, 20) NOT NULL,
	[FA Posting Date] [datetime] NOT NULL,
	[FA Posting Type] [int] NOT NULL,
	[Depreciation Book Code] [nvarchar](10) NOT NULL,
	[Salvage Value] [decimal](38, 20) NOT NULL,
	[Depr_ until FA Posting Date] [tinyint] NOT NULL,
	[Depr_ Acquisition Cost] [tinyint] NOT NULL,
	[Maintenance Code] [nvarchar](10) NOT NULL,
	[Insurance No_] [nvarchar](20) NOT NULL,
	[Budgeted FA No_] [nvarchar](20) NOT NULL,
	[Duplicate in Depreciation Book] [nvarchar](10) NOT NULL,
	[Use Duplication List] [tinyint] NOT NULL,
	[Responsibility Center] [nvarchar](10) NOT NULL,
	[Cross-Reference No_] [nvarchar](20) NOT NULL,
	[Unit of Measure (Cross Ref_)] [nvarchar](10) NOT NULL,
	[Cross-Reference Type] [int] NOT NULL,
	[Cross-Reference Type No_] [nvarchar](30) NOT NULL,
	[Item Category Code] [nvarchar](20) NOT NULL,
	[Nonstock] [tinyint] NOT NULL,
	[Purchasing Code] [nvarchar](10) NOT NULL,
	[Product Group Code] [nvarchar](10) NOT NULL,
	[Return Shipment No_] [nvarchar](20) NOT NULL,
	[Return Shipment Line No_] [int] NOT NULL,
	[Return Reason Code] [nvarchar](10) NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[Price Calculation Method] [int] NOT NULL,
 CONSTRAINT ['+@Company+'$Purch_ Cr_ Memo Line$437dbf0e-84ff-417a-965d-ed2bb9650972$Key1] PRIMARY KEY CLUSTERED 
(
	[Document No_] ASC,
	[Line No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Purch_ Cr_ Memo Line$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END